package org.aom.litebean.module.menu;

import java.util.Map;

import org.aom.entity.menu.Module;
import org.aom.litebean.module.base.BaseEntityFormBean;
import org.aom.service.menu.IModuleManager;
import org.aom.util.FaceUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.annotation.Required;

@ManagedBean(name = "module.menu.moduleFormBean", scope = ManagedBeanScope.REQUEST)
public class ModuleFormBean extends BaseEntityFormBean {

	@Bind(id = "name", attribute = "value")
	@Required(message = "#{this.getMessages().get('required')}")
	private String name;

	private Module module;

	@ManagedProperty("#{moduleManager}")
	private IModuleManager moduleManager;

	@Override
	@Action(id="ok")
	protected void ok() {
		Map<String, Object> session = FaceUtil.getSessionMap();
		if (session.get(ACTION_NAME).toString().equals("add")) {
			module = new Module();
		} else {
			selectedModuleId = (Long) session.get(SELECTED_MODULE_ID);
			module = moduleManager.get(selectedModuleId);
		}
		module.setName(name);
		moduleManager.save(module);
		cancel();
	}

	@Override
	protected void processCancel() {
		name = "";
	}

	@BeforeRender
	protected void beforeRender(boolean isPostBack) {
		initBaseResource();
		Map<String, Object> session = FaceUtil.getSessionMap();
		if (session.get(ACTION_NAME).toString().equals("mod")) {
			selectedModuleId = (Long) session.get(SELECTED_MODULE_ID);
			module = moduleManager.get(selectedModuleId);
			name = module.getName();
		}
	}

}
